package at.fhj.antesk.steamvaluecalc.resources;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class Configs {

	private Properties properties = new Properties();
	private static Configs instance;
	private Set<Entry<Object, Object>> configSet;
	private Map<String, String[]> csvMap = new HashMap<String, String[]>();

	private Configs() {
		try {
			InputStream in = this.getClass().getResourceAsStream(
					"/resources/values.properties");
			properties.load(in);
			configSet = properties.entrySet();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static synchronized Configs getInstance() {
		if (instance == null) {
			instance = new Configs();
		}

		return instance;
	}

	@SuppressWarnings("unchecked")
	public Map<String, String> getConfigsMap() {
		Map<String, String> result = new HashMap<String, String>();

		for (Object entry : configSet) {
			// String cast must be enforced
			String key = (String) ((Entry<Object, Object>) entry).getKey();
			String value = (String) ((Entry<Object, Object>) entry).getValue();
			
			if(value.contains(",")){
				String[] values = value.split(",");
				csvMap.put(key, values);
			}
			
			result.put(key, value);
		}

		return result;
	}

	public Map<String, String[]> getCsvMap() {
		return csvMap;
	}

}
